package core;
import graphics.ImageHandler;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;

import core.*;
import core.actions.*;
import core.units.*;
import core.weapons.*;






public class Player {

	protected PlayerData playerData = null;
	final static double PI = 3.1415;
	protected static int maxChoices = 10;
	protected ActionMap choices = new ActionMap();
	public SingleButtonGame main;
	protected int x, y, size, speed, playerNumber, defaultSpeed;
	protected int hqX = 400, hqY = 400;
	
	protected int type, maxOptions;
	
	protected Point spawnPoint;
	protected Color playerColor = Color.white;
	public int team;
	public char key;
	protected V2Spinner spinner;
	
	protected int energy=100, partEnergy=0;
	protected int maxEnergy=1000;
	protected int energyRegen=10;
	protected boolean isAlive = true;
	protected boolean activationSceduled;
	
	
	/**
	 * 
	 * @param inX Center of ingame menu
	 * @param inY Center of ingame menu
	 * @param inPlayerNumber Unique identifier
	 * @param inKey Key associated with this player
	 * @param inSpawnPoint Default spawnpoint
	 * @param inM Gamecore
	 */
	public Player(int inX, int inY, int inPlayerNumber, char inKey, Point inSpawnPoint, SingleButtonGame inM){
		main = inM;
    	x = inX;
    	y = inY;
    	size = 100;
    	defaultSpeed = 20;
    	playerNumber = inPlayerNumber;
    	key = inKey;
    	team = playerNumber;
    	type = 0;
    	spawnPoint = inSpawnPoint;
    	hqX = spawnPoint.x;
    	hqY = spawnPoint.y;
    	
    	/*int tId = main.units.size();   	
    	main.units.add(new HeroUnit(hqX, hqY, this));
    	selectedUnit = main.units.get(tId);
    	selectedUnit.stop();*/
    	
    	
    	//toMainSpinner();
	}
	public void toMainSpinner(){
		choices=playerData.getMainActionMap();
    	spinner=new V2Spinner(choices, this);
    	//System.out.println(choices);
	}
	
	public void nextFrame(){
		if(isAlive){
			spinner.nextFrame();
			regenEnergy();
			
			if(activationSceduled){
				spinner.click();
				activationSceduled = false;
			}
		}
		
	}
	protected void regenEnergy(){
		partEnergy=partEnergy+energyRegen;
		while(partEnergy>100){
			partEnergy=partEnergy-100;
			if(energy<maxEnergy)
				energy++;
		}
	}
	public void activate(){
		activationSceduled = true;
		//if(isAlive)
		//	spinner.click();
		
/*
		if(spinner.getAction().hasEnoughEnergy()){
			spinner.click();
			if(spinner.getAction().isReady())
				spinner.getAction().activate();
		}*/	
	}
	/*public void paint(Graphics g){
		if(isAlive)
			spinner.paint(g);
	}*/
	public void paint(Point screenOffset, int zoomDivider, Graphics g){
		//Graphics2D g2d = (Graphics2D)g;
		if(isAlive){
			Font pointerFont = new Font("", Font.BOLD, 15);
			g.setFont(pointerFont);
			
			spinner.paint(screenOffset, zoomDivider, g);
			
			ImageHandler.getInstance().drawImage((playerData.selectedUnit.getX()-screenOffset.x)/zoomDivider-20, (playerData.selectedUnit.getY()-screenOffset.y)/zoomDivider-70, 40, 60, "pointer1.png", g);
			
			g.setColor(getColor());
			
			g.setColor(Color.white);
			
			FontMetrics fm = g.getFontMetrics();
			g.drawString(""+Character.toUpperCase(key), (playerData.selectedUnit.getX()-screenOffset.x)/zoomDivider - fm.charWidth(Character.toUpperCase(key))/2, (playerData.selectedUnit.getY()-screenOffset.y)/zoomDivider-45);
			
		}
	}
	public void setActions(ActionMap newActions){
		choices=newActions;
		spinner=new V2Spinner(choices, this);
	}
	public Unit getHero(){
		return playerData.selectedUnit;
	}
	public Color getColor(){
		return playerColor;
	}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	public int getPlayer(){
		return playerNumber;
	}
	public int getTeam(){
		return team;
	}
	public Point getSpawnPoint(){
		return spawnPoint;
	}
	public int getEnergy(){
		return energy;
	}
	public double getPercentEnergy(){
		return (double)energy/maxEnergy;
	}
	public void drainEnergy(int i){
		energy=energy-i;
	}
	public PlayerData getPlayerData(){
		return playerData;
	}
	public void setPlayerData(PlayerData data){
		playerData = data;
		playerData.linkToPlayer(this);
		playerColor=playerData.getPlayerColor();
	}
	public char getKey(){
		return key;
	}
	public void setAlive(boolean b){
		isAlive = b;
	}
	public boolean isAlive(){
		return isAlive;
	}
	public void spawnFirstUnit(){
		playerData.selectedUnit = playerData.getUnits().get(0);
		playerData.selectedUnit.spawn(getSpawnPoint());
		setActions(playerData.selectedUnit.getActionMap());
		playerData.selectedUnit.stop();
    	//spawnAdditionalUnit();
	}
	public void spawnAdditionalUnit(){
		Unit newUnit = playerData.getUnits().get(1);
		Point p = new Point(spawnPoint.x-20000, spawnPoint.y-20000);
		newUnit.spawn(p);
	}
	public void selectUnit(Unit u){
		playerData.selectedUnit=u;
		setActions(playerData.selectedUnit.getActionMap());
		
	}
	public Unit getSelectedUnit(){
		return playerData.selectedUnit;
	}
}







